Fork me on GitHub

sysbench压力测试工具的安装和使用

本文主要介绍了sysbench的安装和使用。

1. sysbench简介

sysbench is a scriptable multi-threaded benchmark tool based on LuaJIT. It is most frequently used for database benchmarks, but can also be used to create arbitrarily complex workloads that do not involve a database server.

sysbench 支持如下的基准测试:

  • oltp_*.lua: a collection of OLTP-like database benchmarks (OLTP数据库 如MySQL、Oracle、PostgreSQL)
  • fileio: a filesystem-level benchmark (文件I/O性能)
  • cpu: a simple CPU benchmark (CPU性能测试)
  • memory: a memory access benchmark (内存访问)
  • threads: a thread-based scheduler benchmark (基于线程的调度性能)
  • mutex: a POSIX mutex benchmark

2. 特点

  • extensive statistics about rate(速率) and latency(延迟) is available, including latency percentiles(统计) and histograms(直方图);
  • low overhead even with thousands of concurrent threads. sysbench is capable of generating and tracking hundreds of millions of events per second;
  • new benchmarks can be easily created by implementing pre-defined hooks in user-provided Lua scripts;

3. 安装

3.1 二进制安装

  • Debian/Ubuntu

    1
    2
    curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.deb.sh | sudo bash
    sudo apt -y install sysbench
  • RHEL/CentOS:

    1
    2
    curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash
    sudo yum -y install sysbench
  • Fedora:

    1
    2
    curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash
    sudo dnf -y install sysbench
  • macOS:

    1
    2
    # Add --with-postgresql if you need PostgreSQL support
    brew install sysbench

3.2 源码构建

推荐使用二进制安装

3.2.1 Build Requirements

Debian/Ubuntu

1
2
3
4
5
apt -y install make automake libtool pkg-config libaio-dev vim-common
# For MySQL support
apt -y install libmysqlclient-dev
# For PostgreSQL support
apt -y install libpq-dev

RHEL/CentOS

1
2
3
4
5
yum -y install make automake libtool pkgconfig libaio-devel vim-common
# For MySQL support, replace with mysql-devel on RHEL/CentOS 5
yum -y install mariadb-devel
# For PostgreSQL support
yum -y install postgresql-devel

Fedora

1
2
3
4
5
dnf -y install make automake libtool pkgconfig libaio-devel vim-common
# For MySQL support
dnf -y install mariadb-devel
# For PostgreSQL support
dnf -y install postgresql-devel

macOS

**Assuming you have Xcode (or Xcode Command Line Tools) and Homebrew installed:

1
2
3
4
5
brew install automake pkg-config
# For MySQL support
brew install mysql
# For PostgreSQL support
brew install postgresql

3.2.2 Build and Install

1
2
3
4
5
6
7
git clone https://github.com/akopytov/sysbench.git
cd sysbench
./autogen.sh
# Add --with-pgsql to build with PostgreSQL support
./configure
make
make install

The above will build sysbench with MySQL support by default. If you have MySQL headers and libraries in non-standard locations (and no mysql_config can be found in the PATH), you can specify them explicitly with --with-mysql-includes and --with-mysql-libs options to ./configure.

To compile sysbench without MySQL support, use --without-mysql. If no database drivers are available database-related scripts will not work, but other benchmarks will be functional.

See README-Oracle.md for instructions on building with Oracle client libraries.

4. 语法说明

4.1 sysbench –help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
[root@localhost ~]# sysbench --help
Usage:
sysbench [options]... [testname] [command]
Commands implemented by most tests: prepare run cleanup help
General options:
--threads=N number of threads to use [1]
--events=N limit for total number of events [0]
--time=N limit for total execution time in seconds [10]
--forced-shutdown=STRING number of seconds to wait after the --time limit before forcing shutdown, or 'off' to disable [off]
--thread-stack-size=SIZE size of stack per thread [64K]
--rate=N average transactions rate. 0 for unlimited rate [0]
--report-interval=N periodically report intermediate statistics with a specified interval in seconds. 0 disables intermediate reports [0]
--report-checkpoints=[LIST,...] dump full statistics and reset all counters at specified points in time. The argument is a list of comma-separated values representing the amount of time in seconds elapsed from start of test when report checkpoint(s) must be performed. Report checkpoints are off by default. []
--debug[=on|off] print more debugging info [off]
--validate[=on|off] perform validation checks where possible [off]
--help[=on|off] print help and exit [off]
--version[=on|off] print version and exit [off]
--config-file=FILENAME File containing command line options
--tx-rate=N deprecated alias for --rate [0]
--max-requests=N deprecated alias for --events [0]
--max-time=N deprecated alias for --time [0]
--num-threads=N deprecated alias for --threads [1]
Pseudo-Random Numbers Generator options:
--rand-type=STRING random numbers distribution {uniform,gaussian,special,pareto} [special]
--rand-spec-iter=N number of iterations used for numbers generation [12]
--rand-spec-pct=N percentage of values to be treated as 'special' (for special distribution) [1]
--rand-spec-res=N percentage of 'special' values to use (for special distribution) [75]
--rand-seed=N seed for random number generator. When 0, the current time is used as a RNG seed. [0]
--rand-pareto-h=N parameter h for pareto distibution [0.2]
Log options:
--verbosity=N verbosity level {5 - debug, 0 - only critical messages} [3]
--percentile=N percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95]
--histogram[=on|off] print latency histogram in report [off]
General database options:
--db-driver=STRING specifies database driver to use ('help' to get list of available drivers)
--db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]
--db-debug[=on|off] print database-specific debug information [off]
Compiled-in database drivers:
mysql - MySQL driver
pgsql - PostgreSQL driver
mysql options:
--mysql-host=[LIST,...] MySQL server host [localhost]
--mysql-port=[LIST,...] MySQL server port [3306]
--mysql-socket=[LIST,...] MySQL socket
--mysql-user=STRING MySQL user [sbtest]
--mysql-password=STRING MySQL password []
--mysql-db=STRING MySQL database name [sbtest]
--mysql-ssl[=on|off] use SSL connections, if available in the client library [off]
--mysql-ssl-cipher=STRING use specific cipher for SSL connections []
--mysql-compression[=on|off] use compression, if available in the client library [off]
--mysql-debug[=on|off] trace all client library calls [off]
--mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205]
--mysql-dry-run[=on|off] Dry run, pretend that all MySQL client API calls are successful without executing them [off]
pgsql options:
--pgsql-host=STRING PostgreSQL server host [localhost]
--pgsql-port=N PostgreSQL server port [5432]
--pgsql-user=STRING PostgreSQL user [sbtest]
--pgsql-password=STRING PostgreSQL password []
--pgsql-db=STRING PostgreSQL database name [sbtest]
Compiled-in tests:
fileio - File I/O test
cpu - CPU performance test
memory - Memory functions speed test
threads - Threads subsystem performance test
mutex - Mutex performance test
See 'sysbench <testname> help' for a list of options for each test.

4.2 命令说明

通用语法

1
sysbench [options]... [testname] [command]

4.2.1 testname

testname is an optional name of a built-in test (e.g. fileio, memory, cpu, etc.), or a name of one of the bundled Lua scripts (e.g. oltp_read_only), or a path to a custom Lua script. If no test name is specified on the command line (and thus, there is no command too, as in that case it would be parsed as a testname), or the test name is a dash (“-“), then sysbench expects a Lua script to execute on its standard input.

  • testname 可以是一个内置的测试 ( 当前支持5种 fileio,memory, cpu,threads, mutex.),
  • 也可以是一个内置的lua脚本名 (e.g. oltp_read_only),
  • a path to a custom Lua script.
  • 不指定的情况, then sysbench expects a Lua script to execute on its standard input.

4.2.2 command

command is an optional argument that will be passed by sysbench to the built-in test or script specified with testname. command defines the action that must be performed by the test. The list of available commands depends on a particular test. Some tests also implement their own custom commands.

Below is a description of typical test commands and their purpose:

  • prepare: performs preparative actions for those tests which need them, e.g. creating the necessary files on disk for the fileio test, or filling the test database for database benchmarks.
  • run: runs the actual test specified with the testname argument. This command is provided by all tests.
  • cleanup: removes temporary data after the test run in those tests which create one.
  • help: displays usage information for the test specified with the testname argument. This includes the full list of commands provided by the test, so it should be used to get the available commands.

4.2.3 options

options 包括通用的选项,另外不同的testname也包含特定的option,如mysql测试包含的数据库连接信息。特定测试的option可以通过sysbench testname help 查看。sysbench --help可以查看通用的option

通用命令行选项

The table below lists the supported common options, their descriptions and default values:

Option Description Default value
--threads The total number of worker threads to create 创建测试线程的数目。默认为1. 1
--events Limit for total number of requests. 0 (the default) means no limit 请求的最大数目。0代表不限制 0
--time Limit for total execution time in seconds. 0 means no limit 最大执行时间,单位是s。 10
–forced-shutdown=STRING number of seconds to wait after the –time limit before forcing shutdown, or ‘off’ to disable [off] 超过–time强制中断。默认是off off
--thread-stack-size Size of stack for each thread 每个线程的堆栈大小。 64K
--rate Average transactions rate. The number specifies how many events (transactions) per seconds should be executed by all threads on average. 0 (default) means unlimited rate, i.e. events are executed as fast as possible 0
--report-interval periodically report intermediate statistics with a specified interval in seconds. 0 disables intermediate reports [0] 执行过程中,显示执行结果的时间间隔 0
--debug Print more debug info 是否显示debug信息 off
--validate Perform validation of test results where possible 在可能情况下执行验证检查。 off
--help Print help on general syntax or on a specified test, and exit 查看帮助 off
--verbosity verbosity level {5 - debug, 0 - only critical messages} [3] 日志详细级别 3
--percentile percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95] 表示设定采样比例,默认是 95%,即丢弃5%的长请求,在剩余的99%里取最大值 95
–histogram[=on\ off] print latency histogram in report [off] report延迟直方图 off
--luajit-cmd perform a LuaJIT control command. This option is equivalent to luajit -j. See LuaJIT documentation for more information

特定测试相关的选项

可以通过sysbench testname help 查看

sysbench fileio help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sysbench fileio help
sysbench 1.0.7 (using bundled LuaJIT 2.1.0-beta2)
fileio options:
--file-num=N number of files to create [128]创建测试文件的数量。默认是128
--file-block-size=N block size to use in all IO operations [16384]测试时文件块的大小。默认是16384(16K)
--file-total-size=SIZE total size of files to create [2G]测试文件的总大小。默认是2G
--file-test-mode=STRING test mode {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw}文件测试模式{seqwr(顺序写), seqrewr(顺序读写), seqrd(顺序读), rndrd(随机读), rndwr(随机写), rndrw(随机读写)}
--file-io-mode=STRING file operations mode {sync,async,mmap} [sync] 文件操作模式{sync(同步),async(异步),fastmmap(快速map映射),slowmmap(慢map映射)}。默认是sync
--file-extra-flags=STRING additional flags to use on opening files {sync,dsync,direct} []使用额外的标志来打开文件{sync,dsync,direct} 。默认为空
--file-fsync-freq=N do fsync() after this number of requests (0 - don't use fsync()) [100]执行fsync()的频率。(0 – 不使用fsync())。默认是100
--file-fsync-all[=on|off] do fsync() after each write operation [off]每执行完一次写操作就执行一次fsync。默认是off
--file-fsync-end[=on|off] do fsync() at the end of test [on]在测试结束时才执行fsync。默认是on
--file-fsync-mode=STRING which method to use for synchronization {fsync, fdatasync} [fsync]使用哪种方法进行同步{fsync, fdatasync}。默认是fsync
--file-merged-requests=N merge at most this number of IO requests if possible (0 - don't merge) [0]如果可以,合并最多的IO请求数(0 – 表示不合并)。默认是0
--file-rw-ratio=N reads/writes ratio for combined test [1.5] 测试时的读写比例。默认是1.5

sysbench cpu help

1
2
3
4
5
sysbench cpu help
sysbench 1.0.7 (using bundled LuaJIT 2.1.0-beta2)
cpu options:
--cpu-max-prime=N upper limit for primes generator [10000] 最大质数发生器数量。默认是10000

sysbench memory help

1
2
3
4
5
--memory-block-size=SIZE size of memory block for test [1K] 测试时内存块大小。默认是1K
--memory-total-size=SIZE total size of data to transfer [100G] 传输数据的总大小。默认是100G
--memory-scope=STRING memory access scope {global,local} [global] 内存访问范围{global,local}。默认是global
--memory-oper=STRING type of memory operations {read, write, none} [write] 内存操作类型。{read, write, none} 默认是write
--memory-access-mode=STRING memory access mode {seq,rnd} [seq] 存储器存取方式{seq,rnd} 默认是seq

sysbench threads help

1
2
3
4
5
6
sysbench threads help
sysbench 1.0.7 (using bundled LuaJIT 2.1.0-beta2)
threads options:
--thread-yields=N number of yields to do per request [1000] 每个请求产生多少个线程。默认是1000
--thread-locks=N number of locks per thread [8] 每个线程的锁的数量。默认是8

sysbench mutex help

1
2
3
4
5
6
7
sysbench mutex help
sysbench 1.0.7 (using bundled LuaJIT 2.1.0-beta2)
mutex options:
--mutex-num=N total size of mutex array [4096] 数组互斥的总大小。默认是4096
--mutex-locks=N number of mutex locks to do per thread [50000] 每个线程互斥锁的数量。默认是50000
--mutex-loops=N number of empty loops to do outside mutex lock [10000] 内部互斥锁的空循环数量。默认是10000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@localhost ~]# sysbench --test=cpu help
sysbench 1.0.7 (using bundled LuaJIT 2.1.0-beta2)
cpu options:
--cpu-max-prime=N upper limit for primes generator [10000]
[root@localhost ~]# sysbench fileio help
sysbench 1.0.7 (using bundled LuaJIT 2.1.0-beta2)
fileio options:
--file-num=N number of files to create [128]
--file-block-size=N block size to use in all IO operations [16384]
--file-total-size=SIZE total size of files to create [2G]
--file-test-mode=STRING test mode {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw}
--file-io-mode=STRING file operations mode {sync,async,mmap} [sync]
--file-async-backlog=N number of asynchronous operatons to queue per thread [128]
--file-extra-flags=STRING additional flags to use on opening files {sync,dsync,direct} []
--file-fsync-freq=N do fsync() after this number of requests (0 - don't use fsync()) [100]
--file-fsync-all[=on|off] do fsync() after each write operation [off]
--file-fsync-end[=on|off] do fsync() at the end of test [on]
--file-fsync-mode=STRING which method to use for synchronization {fsync, fdatasync} [fsync]
--file-merged-requests=N merge at most this number of IO requests if possible (0 - don't merge) [0]
--file-rw-ratio=N reads/writes ratio for combined test [1.5]
[root@localhost ~]# sysbench memory help
sysbench 1.0.7 (using bundled LuaJIT 2.1.0-beta2)
memory options:
--memory-block-size=SIZE size of memory block for test [1K]
--memory-total-size=SIZE total size of data to transfer [100G]
--memory-scope=STRING memory access scope {global,local} [global]
--memory-hugetlb[=on|off] allocate memory from HugeTLB pool [off]
--memory-oper=STRING type of memory operations {read, write, none} [write]
--memory-access-mode=STRING memory access mode {seq,rnd} [seq]
[root@localhost ~]# sysbench threads help
sysbench 1.0.7 (using bundled LuaJIT 2.1.0-beta2)
threads options:
--thread-yields=N number of yields to do per request [1000]
--thread-locks=N number of locks per thread [8]

参考

sysbench 安装、使用和测试

https://github.com/akopytov/sysbench

好记性不如烂笔头,生命不息,学习不止!

分享